<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="${pageContext.request.contextPath }/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
	$(function(){
		$("#name").blur(function(){
			var url="${pageContext.request.contextPath }/emp/name/"+this.value;
			$.get(url,{},function(response){
				alert(JSON.stringify(response));
				if(response.code==0){
					if (response.data==0) {
						$("#nameValid").html("名称可用");
					}else{
						$("#nameValid").html("名称不可用");
					}
			}else{
				alert("请联系管理员")
			}
			});
		});
	});
	
	$(function(){
		$("#sal").blur(function(){
			var a=this.value;
			var url="${pageContext.request.contextPath }/emp/sal"
			$.get(url,{},function(response){
				alert("sss");
				if(response.code==0){
					if (response.sally>a) {
						$("#salValid").html("工资可用");
					}else{
						$("#salValid").html("工资不可用");
					}
			}else{
				alert("请联系管理员")
			}
			});
		});
	});
</script>

</head>
<body>
	<!-- modelAttribute的作用:将表单和对象绑定 -->
	<form:form action="${pageContext.request.contextPath}/emp" method="post" modelAttribute="emp">
		<c:if test="${emp.empId==null }">
			<h1>新增员工</h1>
		</c:if>
		<c:if test="${emp.empId!=null }">
			<h1>修改员工</h1>
			<form:hidden path="empId"/><!-- 隐藏域 -->
			<!-- 待转换的表单提交方式 -->
			<input type="hidden" name="_method" value="PUT">
		</c:if>
		<p>姓名：<form:input path="name"/> <form:errors path="name"></form:errors>
			<span id="nameValid"></span>
		</p>
		<p>工资：<form:input path="sal"/><form:errors path="sal"></form:errors>
			<span id="salValid"></span>
		</p>
		<p>生日：<form:input path="birthday"/></p>
		<p>部门：
			<!-- 
			<select name="deptId">
				<option>-请选择-</option>
				<c:forEach items="${deptList}" var="dept">
					<option value="${dept.deptId }">${dept.deptName}</option>
				</c:forEach>
			</select>
			 -->
			<form:select path="deptId" items="${deptList }" itemLabel="deptName" itemValue="deptId"></form:select>
		</p>
		<button>保存</button>
	</form:form>
</body>
<!-- 
 <h1>新增员工</h1>
	<form action="${pageContext.request.contextPath}/emp" method="post">
		<p>姓名：<input type="text" name="name"></p>
		<p>工资：<input type="text" name="sal"></p>
		<p>部门：
			<select name="deptId">
				<option>-请选择-</option>
				<c:forEach items="${deptList}" var="dept">
					<option value="${dept.deptId }">${dept.deptName}</option>
				</c:forEach>
			</select>
		</p>
		<button>保存</button>
	</form>-->
</html>
